/**
 * 
 */
package com.bestbuy.daoimple;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.time.DateFormatUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.bestbuy.dao.CartDao;
import com.bestbuy.pojo.Account;
import com.bestbuy.pojo.Bill;
import com.bestbuy.pojo.Cart;
import com.bestbuy.pojo.CartDetail;
import com.bestbuy.pojo.Product;
import com.bestbuy.service.AccountService;
import com.bestbuy.service.ProductService;

/**
 * @author Tuan
 *
 */
@Repository
@SuppressWarnings({ "unchecked", "rawtypes" })
public class CartDaoImpl implements CartDao{

	@Autowired 
	ProductService productDao;
	
	@Autowired
	AccountService accountDao;
	
	@Autowired
	private SessionFactory sessionFactory;
	/* (non-Javadoc)
	 * @see com.bestbuy.dao.CartDao#getCart(java.lang.String)
	 */
	public Cart getCart(String username) {
		Session session = sessionFactory.getCurrentSession();
		String sql = "from Cart ca where ca.account.username =:taikhoan";
		Query query = session.createQuery(sql);
		query.setString("taikhoan", username);
		List<Cart> carts = (List<Cart>) query.list();
		Cart cart = null;
		for(int i=0;i<carts.size();i++)
		{
			if(carts.get(i).getIsdeleted()==0)
				cart = carts.get(i);
		}
		return cart;
	}
	/* (non-Javadoc)
	 * @see com.bestbuy.dao.CartDao#getCartDetail(java.lang.Integer)
	 */
	public List<CartDetail> getCartDetail(Integer idCart) {
		Session session = sessionFactory.getCurrentSession();
		String sql = "from CartDetail ca where ca.cart.idcart =:idcart";
		Query query = session.createQuery(sql);
		query.setString("idcart", idCart.toString());
		List<CartDetail> cartDetail = (List<CartDetail>) query.list();
		return cartDetail;
	}
	/* (non-Javadoc)
	 * @see com.bestbuy.dao.CartDao#addToCart(java.lang.Integer, java.lang.Integer, java.lang.Integer)
	 */
	public Boolean addToCart(String username, Integer idProduct, Integer quantity) {
		Session session = sessionFactory.getCurrentSession();
		
		Cart cart = this.getCart(username);
		List<CartDetail> cartDetails = this.getCartDetail(cart.getIdcart());
		Boolean flagadd = true;
		for(int i=0;i<cartDetails.size();i++)
		{
			if(cartDetails.get(i).getProduct().getIdproduct()==idProduct && cartDetails.get(i).getCart().getIdcart()==cart.getIdcart())
			{
				flagadd=false;
			}
		}
		if(flagadd)
		{
			Product product = productDao.getProduct(idProduct);
			CartDetail cartdetail = new CartDetail();
			cartdetail.setCart(cart);
			cartdetail.setIsdeteled(0);
			cartdetail.setQuantity(quantity);
			cartdetail.setProduct(product);
			session.save(cartdetail);
			return true;
		}
		else
		{
			String sql = "from CartDetail ca where ca.cart.idcart =:idcart and ca.product.idproduct =:idproduct";
			Query query = session.createQuery(sql);
			query.setString("idcart", cart.getIdcart().toString());
			query.setString("idproduct", idProduct.toString());
			List<CartDetail> cartDetail = (List<CartDetail>) query.list();
			CartDetail cartupdate = cartDetail.get(0);
			cartupdate.setQuantity(cartupdate.getQuantity()+1);
			session.update(cartupdate);
			return false;
			
		}
		
		
	}
	/* (non-Javadoc)
	 * @see com.bestbuy.dao.CartDao#removeProduct(java.lang.String)
	 */
	public Boolean removeProduct(String idCartDetail) {
		Session session = sessionFactory.getCurrentSession();
		String sql = "from CartDetail ca where ca.idcartdetail =:idCartdetail";
		Query query = session.createQuery(sql);
		query.setString("idCartdetail", idCartDetail);
		CartDetail cartdetail = (CartDetail)query.uniqueResult();
		session.delete(cartdetail);
		return true;
	}
	/* (non-Javadoc)
	 * @see com.bestbuy.dao.CartDao#updateProduct(java.lang.String, java.lang.String)
	 */
	public Boolean updateProduct(String idCartDetail, String quantity) {
		Session session = sessionFactory.getCurrentSession();
		String sql = "from CartDetail ca where ca.idcartdetail =:idCartdetail";
		Query query = session.createQuery(sql);
		query.setString("idCartdetail", idCartDetail);
		CartDetail cartdetail = (CartDetail)query.uniqueResult();
		cartdetail.setQuantity(Integer.parseInt(quantity));
		session.update(cartdetail);
		return true;
	}
	/* (non-Javadoc)
	 * @see com.bestbuy.dao.CartDao#saveBill(java.lang.Integer, java.lang.Integer)
	 */
	public Boolean saveBill(String username) {
		Session session = sessionFactory.getCurrentSession();
		Bill bill = new Bill();
		Account account = accountDao.GetAccount(username);
		Cart cart = this.getCart(username);
		bill.setAccount(account);
		bill.setCart(cart);
		Date date = new Date();
		bill.setDatecreated(date);
		bill.setIsdeleted(0);
		bill.setIspaid(0);
		cart.setIsdeleted(1);
		session.save(bill);
		cart.setIsdeleted(1);
		session.update(cart);
		
		//tao cart moi cho khach hang
		
		Cart cartnew = new Cart();
		cartnew.setAccount(account);
		cartnew.setIsdeleted(0);
		cartnew.setTotal(0.0f);
		session.save(cartnew);
		return null;
	}
	/* (non-Javadoc)
	 * @see com.bestbuy.dao.CartDao#updateCart(com.bestbuy.pojo.Cart)
	 */
	public void updateCart(Cart cart) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		session.update(cart);
	}
}
